package listenerUtil;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicStampedReference;

public class ListenContainer<T> {

    //变量
    //响应容器
    private static volatile AtomicReferenceArray atomicReferenceArray;

    private static volatile AtomicReference<String> stringAtomicReference;

    public ListenContainer() {
        this.atomicReferenceArray = new AtomicReferenceArray(1);
        this.stringAtomicReference = new AtomicReference<>();
        this.atomicReferenceArray.set(0,null);
    }


    public static AtomicReference<String> getStringAtomicReference() {
        return stringAtomicReference;
    }

    public static AtomicReferenceArray getAtomicReferenceArray() {
        return atomicReferenceArray;
    }

    public void addArrayElement(T element) {
        synchronized (this){
            while (!atomicReferenceArray.compareAndSet(0,null,element)){

            }
        }
    }

    public void addStringElement(String element) {
        synchronized (this){
            while (!stringAtomicReference.compareAndSet(null,element)){

            }
        }
    }
}
